
#ifndef _SPIFLASH_
#define _SPIFLASH_

#include "ac309n.h"
#include "typedef.h"
#include "uart.h"
//#include "STRING.H"

typedef struct __ADDR_RW
{
u8 xdata *buf ;
u32 addr ;
u16 len ;

}RW_ADDR ;

typedef	struct
{
	u32 addr;
	u32 len;
	u32 ptr ;
}FLASH_FILE;


#define    F_READ       0x03
#define    F_DUAL_READ  0x3B
#define    F_PROG       0x02
#define	   F_RDSR       0x05
#define    F_WRSR       0x01
#define    F_WREN       0x06
#define    F_WRDIS      0x04
#define    F_RDID       0x90    //0x9F 0xAB
#define    F_ERASSEC    0xD8    //0x20


#define    FC_READ    F_READ
#define    FC_PROG    F_PROG
#define    FC_RDSR    F_RDSR
#define    FC_WRSR    F_WRSR
#define    FC_WREN    F_WREN
//#define		FC_WRDIS	F_WRDIS
#define    FC_RDID    F_RDID	

#define    SEC_ERASE   0x20
#define    BLO_ERASE   0xD8
#define    CHIP_ERASE  0xC7


	  

#define FLASH_CS_H()	 P3 |= BIT(0)
#define FLASH_CS_L()	 	P3 &= ~BIT(0)

#define FLASH_CS_OUT()   P3DIR &= ~BIT(0)
#define FLASH_CS_EN()	 FLASH_CS_L()
#define FLASH_CS_DIS()	 FLASH_CS_H()

#define FLASH_DATA_OUT() P1DIR &= ~BIT(7)
#define FLASH_DATA_IN()  P1DIR |= BIT(7)
#define FLASH_DATA_L() P1 &= ~BIT(7)
#define FLASH_DATA_H()  P1 |= BIT(7)
#define FLASH_DATA_R()  (P1 & BIT(7))


#define FLASH_CLK_OUT()  P1DIR &= ~BIT(6)
#define FLASH_CLK_IN()   P1DIR |= BIT(6)
#define FLASH_CLK_L()   P1 &= ~BIT(6)
#define FLASH_CLK_H()   P1 |= BIT(6)



#define SPIRX() //SPICON |= BIT(2)
#define SPITX() //SPICON &= ~BIT(2)

extern void SPIInit(void) ;
extern void bspspiFlashWrite(RW_ADDR xdata *  rwaddr);
//void bspspiFlashRead(u8 xdata *buf ,u16 len ) ;//
extern void bspspiFlashRead(RW_ADDR xdata *  rwaddr);
extern void spiFlashSecEraser(u32 address)	  ;
extern void spiFlashBlockEraser(u32 address);
extern void spiFlashChipEraser();
extern u8  GetByteSPI(void);
extern void SPIOutSeclect(u8 ch);
extern void SendByteSPI(u8  Spidata) ;
 extern u8 SPIFlashInit() ;
 extern u16 Crc16(u8 xdata *ptr, u16 xdata len) ;
  extern void spiFlashReadSetAddr (u32    address) ;
 extern void spiFlashRead(u8  _xdata *buf ,u32    address,u16   len) ;
 extern void spiFlashWrite(u8 _xdata *spidata,u32 xdata   address,u16 xdata  len);

#endif

